import request from '@/http/request';
import {  ElMessage } from "element-plus";


// -----------文件相关接口----------
export const fileApi={
    uploadImg: (param) => request.post(`bf-assets/file/upload/img`, param), // 上传图片
    uploadDocument: (param) => request.post(`bf-assets/file/upload/document`, param), // 上传文档
    fileDownload:(param)=> request.get('bf-assets/file/download',{ params:param, responseType: "blob"}),//文件下载
    deleteFile:(param)=>request.get(`bf-assets/file/download?associationId=${param.associationId}&urlKey=${param.urlKey}`), // 删除文件
}

/* 文件下载回调 */
export const fileCallback = (param)=>{
  fileApi
  .fileDownload({
    fileName: param.fileName,
    urlKey: param.fileUrlKey,
  })
  .then((res) => {
    if (res) {
      let blob = new Blob([res], {
        type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",
      });
      let a = document.createElement("a");
      a.href = URL.createObjectURL(blob);
      a.download = param.fileName;
      a.click();
      URL.revokeObjectURL(a.href)
      a.remove()
    } else {
      ElMessage.error('文件导出失败')
    }
  });
}


export default {
    fileApi
}